package com.UI;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import com.Model.StudentDBModel;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/** Author : Mohammed
 * 
 */


public class EditCourseActivity extends Activity {
	/** Called when the activity is first created. */
	StudentDBModel model;
	ArrayList<String> cDetails;
	String[] courseDetails;
	ContentValues editData;
	String courseNo;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_course);
		
       	Bundle extras = getIntent().getExtras();
    	courseNo = extras.getString("CourseNo");
    	
    	model = new StudentDBModel(this);
    	
    	cDetails = model.getCourseDetails(courseNo);

    	courseDetails = (String[]) cDetails.toArray(new String[cDetails.size()]);

    	
    	
		TextView showCourseNo = (TextView)findViewById(R.id.showCourseNo);
		
		showCourseNo.setText(courseNo);
		
		EditText editCourseTitle = (EditText)findViewById(R.id.editCourseTitle);
		
		editCourseTitle.setText(courseDetails[1]);
		
		EditText editSectionNo = (EditText)findViewById(R.id.editSectionNo);
		
		editSectionNo.setText(courseDetails[2]);
		
		EditText editCourseLocation = (EditText)findViewById(R.id.editCourseLocation);
		
		editCourseLocation.setText(courseDetails[3]);
		
		EditText editCourseCredit = (EditText)findViewById(R.id.editCourseCredit);
		
		editCourseCredit.setText(courseDetails[4]);
		
		EditText editCourseEmail = (EditText)findViewById(R.id.editCourseEmail);
		
		editCourseEmail.setText(courseDetails[5]);
		
		EditText editCourseWebsite = (EditText)findViewById(R.id.editCourseWebsite);
		
		editCourseWebsite.setText(courseDetails[6]);
		

    	
        // register this activity as a button event listener
        Button btnEdit = (Button)findViewById(R.id.btnEdit);
         btnEdit.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
    	
	        		// collect the course data
	        		 editData = new ContentValues();
	        		
		        		EditText editCourseTitle = (EditText)findViewById(R.id.editCourseTitle);
		        		
		        		String tempCourseTitle = editCourseTitle.getText().toString().replaceAll(" ", "");
		        		
		        		
		        		if(tempCourseTitle.length() == 0 ) {
		        	      
		        		  AlertDialog alertDialog = new AlertDialog.Builder(EditCourseActivity.this).create();		        			
		        		  alertDialog.setTitle("Warning");		        		  
		        		  alertDialog.setMessage("Title can't be blank!");		        		 		        		  
		        		  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		        		  
		        		  public void onClick(DialogInterface dialog, int which) {
		        		  
		        		       return;
		        		  
		        		      } }); 
		        		  
		        		  alertDialog.show();
		        		  
		        		}
		        		

		        		
		        		
		        		
		        		else{	
	        		
	        		editData.put("CourseName", editCourseTitle.getText().toString());
	        		EditText editSectionNo = (EditText)findViewById(R.id.editSectionNo);
	        		editData.put("Section", editSectionNo.getText().toString());
	        		EditText editCourseCredit = (EditText)findViewById(R.id.editCourseCredit);
	        		editData.put("Credit", editCourseCredit.getText().toString());
	        		EditText editCourseEmail = (EditText)findViewById(R.id.editCourseEmail);
	        		editData.put("ProfEmail", editCourseEmail.getText().toString());
	        		EditText editCourseWebsite = (EditText)findViewById(R.id.editCourseWebsite);
	        		editData.put("Website", editCourseWebsite.getText().toString());
	        		EditText editCourseLocation = (EditText)findViewById(R.id.editCourseLocation);
	        		editData.put("Location", editCourseLocation.getText().toString());
	        		

	        		// use the model to store the course data
	        		model.updateCourses(editData,courseNo);
	        		
	        		
	        		//once the course is added, redirect the user to the View Courses page
	        		Intent viewCourse = new Intent(EditCourseActivity.this, CourseList.class);
					startActivity(viewCourse);
					EditCourseActivity.this.finish();
	        		
               }
             }              
         });
		
	}

    /**
     * Validates if input String for courseId is a number
     */
    public boolean checkIfNumber(String in) {
        
        try {

            Integer.parseInt(in);
        
        } catch (NumberFormatException ex) {
            return false;
        }
        
        return true;
    }
	
	
}
